package com.ff.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.common.R;
import com.ff.entity.Category;
import com.ff.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 分类管理
* */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    /*
    * 查询分类管理信息
    * */
    @GetMapping("/page")
    public  R<Page> page(Integer page,Integer pageSize){

        Page<Category> pageInfo = new Page<>(page, pageSize);

        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();

        queryWrapper.orderByAsc("sort");

        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
    /*
    * 根据id删除删除分类
    * */
    @DeleteMapping
    public  R<String> delete(Long ids){
        //categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (category.getType()!=null){
            queryWrapper.eq("type",category.getType());
        }
        queryWrapper.orderByAsc("sort");
        queryWrapper.orderByDesc("update_time");
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
